#include "bulletManager.h"


bulletManager* bulletManager::_instance = 0;

bulletManager* bulletManager::Instance()
{
	if (_instance == NULL)
	{
		_instance = new bulletManager();
	}
	return _instance;
}

bulletManager::bulletManager()
{
	num = 0;
	for (int i = 0; i < 1000; i++)
	{
		freeMem.push_back(new Bullet());
	}
}

void bulletManager::updata()
{
	for (list<Bullet*>::iterator itor = manager.begin();
		itor != manager.end();)
	{
		if ((*itor)->checkRemove())
		{
			/*delete *itor;
			itor = manager.erase(itor);*/
			list<Bullet*>::iterator itor1 = itor;
			itor1++;
			freeMem.splice(freeMem.begin(), manager, itor);
			itor = itor1;
		}else
		{
			(*itor)->updata();
			itor++;
		}
	}
	//cout << freeMem.size() << " " << manager.size() << endl;
}

void bulletManager::draw()
{
	for (list<Bullet*>::iterator itor = manager.begin();
		itor != manager.end(); itor++)
	{
		(*itor)->draw();
	}
}

/*
void bulletManager::add(Bullet* b)
{
	manager.push_front(b);
}*/

void bulletManager::add(stdBullet lib, Point p, Point* player)
{
	if (freeMem.empty())
	{
		cout << "the freeMem is null" << endl;
		return;
	}
	list<Bullet*>::iterator itor = freeMem.begin();
	(*itor)->setInformation(lib, p, player);
	manager.splice(manager.begin(), freeMem, itor);
}

void bulletManager::add(stdBullet lib, Point p, Point* player, int i)
{
	if (freeMem.empty())
	{
		cout << "the freeMem is null" << endl;
		return;
	}
	list<Bullet*>::iterator itor = freeMem.begin();
	(*itor)->setInformation(lib, p, player, i);
	manager.splice(manager.begin(), freeMem, itor);
}

void bulletManager::add(stdBullet lib, Point p, Point* player, int i, int effect, Point *round)
{
	if (freeMem.empty())
	{
		cout << "the freeMem is null" << endl;
		return;
	}
	list<Bullet*>::iterator itor = freeMem.begin();
	(*itor)->setInformation(lib, p, player, i, effect, round);
}

void bulletManager::clear()
{
	freeMem.splice(freeMem.begin(), manager);
}

void bulletManager::checkHit(Player& p)
{
	for (list<Bullet*>::iterator itor = manager.begin();
		itor != manager.end(); itor++)
	{
		if(p.checkHit((*itor)->getNowPoint(), (*itor)->getRange()))
		{
			(*itor)->changeRemove();
		}
	}
}